(function (){
    window.autocadUpload = {
        xhrReq:function (method,url,data,progress,headerMap,xhr,responseType){
            if(!xhr){
                xhr = new XMLHttpRequest();
            }
            return new Promise(function (success,error){
                xhr.open(method, url);
                if(responseType){
                    xhr.responseType = responseType;
                }
                if(headerMap){
                    for(let key in headerMap){
                        let val = headerMap[key];
                        xhr.setRequestHeader(key,val);
                    }
                }
                xhr.addEventListener('loadend', function() {
                    if(xhr.readyState===4) {
                        success(xhr);
                    }
                });
                xhr.addEventListener('error', function() {
                    error();
                });
                xhr.addEventListener('progress', function(evt) {
                    if(progress){
                        progress(evt.loaded,evt.total,2);
                    }
                });
                xhr.upload.addEventListener('progress', function(evt) {
                    if(progress){
                        progress(evt.loaded,evt.total,1);
                    }
                });
                try{
                    xhr.send(data);
                }catch (e){
                    error();
                }
            });
        },
        upload:async function (param,progress){
            let that = this;
            let data = await autocad.uploadPre(param.path,param.filePath,JSON.stringify({}));
            try{
                param.uploadPreRes = JSON.parse(data.data);
            }catch (e){
            }
            if(!param.uploadPreRes){
                param.status = 3;
                param.msg = "上传出错";
                param.statusChange(param);
                return;
            };
            let uploadUrl = param.uploadPreRes.url;
            param.currentXhr = new XMLHttpRequest();
            let xhr = await that.xhrReq("PUT",uploadUrl,param.file,progress,
                {
                    "Content-Type":""
                },param.currentXhr);
            if(xhr.status != 200){
                param.status = 3;
                param.msg = "上传状态码错误:"+xhr.status;
                return;
            }
            let check = await autocad.uploadAfter(param.path, param.filePath,JSON.stringify({
                objectName:param.uploadPreRes.objectName,
                uploadKey:param.uploadPreRes.uploadKey,
                isServer:true
            }));
            let errMsg = check.msg;
            const checkStr = check.data;
            if(checkStr){
                param.status = 2;
                param.msg = "上传成功";
                param.uploadPreRes.fileId = checkStr;
            }else{
                param.status = 3;
                param.msg = errMsg?errMsg:"上传出错";
            }
            param.statusChange(param);
        }
    };
})()